package com.lxl.chat.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 * @describe: druid连接池配置类
 * @author: lxl
 * @date: 2018年10月22日11:27:33
 */
@Slf4j
@Configuration
public class DruidConfig {

    /**
     * 数据源配置
     *
     * @return 数据源对象
     */
    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }


    /**
     * druid监控面板配置
     *
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 白名单(只有这台主机能访问druid 的web界面)
        registrationBean.addInitParameter("allow", "127.0.0.1");
        // 黑名单
        registrationBean.addInitParameter("deny", "");
        registrationBean.addInitParameter("loginUsername", "登陆druid的用户名");
        registrationBean.addInitParameter("loginPassword", "登陆druid的密码");
        registrationBean.addInitParameter("resetEnable", "false");
        return registrationBean;
    }

    /**
     * druid访问路径和排除资源配置
     *
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> druidWebStatViewFilter() {
        FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        registrationBean.addInitParameter("urlPatterns", "/*");
        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        return registrationBean;
    }
}
